// pages/register/register.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reg_hqyzm: "获取验证码",
    countdown_time: 60,
    showtime1: true,
    showtime2: false,
    user_type:1,
    phone:'',
    code:''
  },
  reg_yanzhengma_huoqu: function () {
    var that = this;
    var phone = that.data.phone;
    util.commonRequest('verification', 'post', {phone:phone}, function (res) {
      if (res.data.code == 200) {
        that.setData({
          showtime1: false,
          showtime2: true,
        });
        countdown(that);
        wx.showToast({
          title: '发送成功',
        })
       
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
 
   
  },
  inputPhone:function(e){
    var phone = e.detail.value;
    this.setData({
      phone:phone
    })
  },
  inputCode:function(e){
    var yaCode = e.detail.value;
    this.setData({
      code:yaCode
    })
  },
  onGotUserInfo(e) {
    var that = this;
    var phone = that.data.phone;
    var auth = that.data.code;//55059
    if (!phone) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      })
      return false;
    }if (!auth) {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
      return false;
    } else if (!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(phone))) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }else {
     
      let param = {
        phone: phone,
        yacode: auth,
        user_type:that.data.user_type
      };
      util.commonRequest('checkRegisterUser', 'post', param, function (res) {
        if (res.data.code == 200) {
            wx.showToast({
              title: '验证成功，设置密码',
              icon: 'success',
              duration: 1500
            })
            setTimeout(function () {				//确认之后跳转的页面
              wx.navigateTo({
                url: '../possword/possword?phone=' + res.data.data + "&type=" + that.data.user_type,
              })
            }, 1500);
        } else {
          wx.showToast({
            icon: 'loading',
            title: res.data.msg,
          })
        }
      });
    
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user_type = options.type;
    this.setData({
      user_type:user_type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function countdown(that) {
  
  var second = that.data.countdown_time;
  if (second == 0) {
    console.log("001");
    that.setData({
      showtime1: true,
      showtime2: false,
      reg_hqyzm: "重新获取",
      countdown_time: 60,
    });
    console.log("002");
    return;
  }
  var time = setTimeout(function () {
    that.setData({
      countdown_time: second - 1
    });
    countdown(that);
  },1000)
 
}
// function sendCode(){
//   // verification'
// }